﻿using System;
using Utilities;
using Business;

namespace TimeWork.webmaster
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorEmail.Visible = false;
            ErrorPass.Visible = false;
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            string email = TxtEmail.Text.ToString();
            string pass = UtiGeneralClass.Md5Encode(TxtPass.Text.ToString());

            int result = BuStaff.CheckSignIn(email, pass);

            switch (result)
            {
                case 1:
                    ErrorEmail.Visible = true;
                    ErrorEmail.Text = "Email không đúng";
                    break;
                case 2:
                    ErrorPass.Visible = true;
                    ErrorPass.Text = "Mật khẩu không đúng";
                    break;
                case 0:
                    if (remember.Checked)
                    {
                        Response.Cookies["Name"].Value = email;
                        Response.Cookies["Pass"].Value = TxtPass.Text.ToString();
                        Response.Cookies["Name"].Expires = DateTime.Now.AddMonths(2);
                        Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(2);
                    }
                    else
                    {
                        Response.Cookies["Name"].Expires = DateTime.Now.AddMonths(-1);
                        Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(-1);
                    }
                    Session.Timeout = 1440;
                    Session["mail"] = email;
                    Response.Redirect("http://" + Request.Url.Authority + "/webmaster/Default.aspx");
                    break;
                default:
                    ErrorEmail.Visible = true;
                    ErrorEmail.Text = "Thử đăng nhập lại";
                    break;
            }
        }
    }
}